<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "project_ease".
 *
 * @property string $id
 * @property string $code 项目编号
 * @property string $title 项目名称
 * @property string $package_id 所属资产包
 * @property string $borrower 借款人
 * @property int $province 省份
 * @property int $city 城市
 * @property int $category 类型
 * @property string $principal 本金
 * @property string $interest 利息
 * @property int $is_highlights 是否为亮点项目
 * @property string $highlights 亮点明细
 * @property int $is_suit 诉讼状态
 * @property string $stakeholders 保证人/抵押人
 * @property int $management_status 借款人状况
 * @property string $stakeholders_status 保证人经营状况
 * @property string $mortgage_principal 抵押本金（元）
 * @property string $mortgage_interest 抵押利息（元）
 * @property string $mortgage_owner 权属人
 * @property string $mortgage_type 抵押物类型
 * @property string $other_warrants_code 他项权证号
 * @property string $mortgage_address 抵押物地址
 * @property string $mortgage_building_area 建筑面积（平米）
 * @property string $mortgage_land_area 土地面积（平米）
 * @property string $mortgage_detail 抵押物详情描述
 * @property string $created_at 创建时间
 */
class ProjectEase extends \yii\db\ActiveRecord
{
    public static function getDb()
    {
        return Yii::$app->get('forge');
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'project_ease';
    }

    public function attributes()
    {
        return array_merge(parent::attributes(), ['tagTitle', 'columns']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['package_id', 'borrower', 'province', 'city', 'category', 'principal', 'stakeholders',], 'required'],
            [['principal', 'interest'], 'trim'],
            [['principal', 'interest'], 'double'],
            [['package_id', 'province', 'city', 'category', 'is_highlights', 'is_suit', 'management_status', 'level', 'owner', 'source',], 'integer'],
            [['management_status', 'stakeholders_status', 'interest', 'mortgage_principal', 'mortgage_interest', 'source',], 'default', 'value' => 0],
            [['highlights', 'mortgage_detail', 'suit', 'execution', 'detail', 'note', 'follow',], 'string'],
            [['highlights', 'mortgage_detail', 'follow',], 'default', 'value' => ''],
            [['created_at', 'stakeholders_status', 'published_at', 'tag', 'tagTitle', 'updated_at', 'owner',], 'safe'],
            [['mortgage_principal', 'mortgage_interest',], 'trim'],
            [['mortgage_principal', 'mortgage_interest',], 'double'],
            [['code', 'title', 'borrower', 'stakeholders', 'mortgage_owner', 'mortgage_type', 'other_warrants_code', 'mortgage_address', 'mortgage_building_area', 'mortgage_land_area'], 'string', 'max' => 255],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => '项目编号',
            'title' => '项目名称',
            'package_id' => '隶属资产包',
            'borrower' => '借款人',
            'province' => '省份',
            'city' => '城市',
            'category' => '担保方式',//原项目类型
            'principal' => '本金（元）',
            'interest' => '利息（元）',
            'is_highlights' => '是否亮点',
            'highlights' => '亮点说明',
            'is_suit' => '是否诉讼',
            'stakeholders' => '保证人/抵押人',
            'management_status' => '借款人状况',
            'stakeholders_status' => '保证人/抵押人状况',
            'mortgage_principal' => '抵押本金（元）',
            'mortgage_interest' => '抵押利息（元）',
            'mortgage_owner' => '权属人',
            'mortgage_type' => '抵押物类型',
            'other_warrants_code' => '他项权证号',
            'mortgage_address' => '抵押物地址',
            'mortgage_building_area' => '建筑面积（㎡）',
            'mortgage_land_area' => '土地面积（㎡）',
            'mortgage_detail' => '抵押物详情描述',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',

            'level' => '项目评级',
            'suit' => '诉讼情况',
            'execution' => '执行情况',
            'tag' => '涉及资产标签',
            'source' => '涉及资产来源',
            'detail' => '资产详情描述',
            'note' => '项目说明',//沟通情况
            'follow' => '跟进方向',//过会备注
            'owner' => '上传人'
        ];
    }

    /**
     * @inheritdoc
     * @return ProjectEaseQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new ProjectEaseQuery(get_called_class());
    }

    public function getPackage()
    {
        return $this->hasOne(PackageEase::className(), ['id' => 'package_id']);
    }

    public function getAllPackages()
    {
        $arr_packages = PackageEase::find()->where(['owner' => Yii::$app->user->id])->asArray()->all();
        return ArrayHelper::map($arr_packages, 'id', 'title');
    }

    public function getPublisher()
    {
        return $this->hasOne(User::className(), ['id' => 'owner']);
    }
}
